import 'package:flutter/material.dart';
import 'package:x3_component_core/components/selector_components/selector_template/base_bottom_sheet_multip_template.dart';
import 'package:x3_component_core/controller/base_bottom_sheet_multiple_controller.dart';
import 'package:x3_component_core/models/base_bottom_sheet_selector_model.dart';
import 'package:x3_component_core/utils/base_component_utils.dart';

class BaseBottomSheetMultipleSelector extends StatefulWidget {
  /// 控制器
  final BaseBottomSheetMultipleController controller;

  /// 完成事件
  final Function(List<BaseBottomSheetSelectorModel>) onComplete;

  /// 是否显示 icon
  final bool isIcon;

  /// 是否禁用
  final bool disable;

  /// 内边距
  final EdgeInsets padding;

  /// 装饰item
  final Widget Function(BaseBottomSheetSelectorModel, bool) decorator;

  const BaseBottomSheetMultipleSelector({
    Key key,
    @required this.controller,
    @required this.onComplete,
    this.isIcon = true,
    this.disable = false,
    this.padding = EdgeInsets.zero,
    this.decorator,
  })
      : assert(controller != null),
        super(key: key);

  @override
  _BaseBottomSheetMultipleSelectorState createState() => _BaseBottomSheetMultipleSelectorState();
}

class _BaseBottomSheetMultipleSelectorState extends State<BaseBottomSheetMultipleSelector> {
  /// 数据列
  List<BaseBottomSheetSelectorModel> get _items => widget.controller.items;

  @override
  void initState() {
    widget.controller.addListener(() {
      _onShowModalBottomSheet();
    });
    super.initState();
  }

  @override
  void dispose() {
    widget.controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AbsorbPointer(
      absorbing: widget.disable,
      child: InkWell(
        onTap: _items.length == 0 ? null : _onShowModalBottomSheet,
        child: Container(
          height: 48,
          padding: widget.padding,
          child: Row(
            children: [
              Expanded(child: Container(alignment: Alignment.centerLeft, child: _buildText())),
              widget.isIcon == true ? _buildIcon() : SizedBox(),
            ],
          ),
        ),
      ),
    );
  }

  /// 创建 icon
  Widget _buildIcon() {
    return Container(alignment: Alignment.centerLeft, child: Icon(Icons.arrow_drop_down, color: Color(0xFFCCCCCC), size: 18));
  }

  /// 文本显示
  Widget _buildText() {
    return Text(
      widget.controller.selectedText ?? '',
      style: (widget.controller.selectedText.isNotEmpty && widget.controller.selectedText == "请选择") ? Theme
          .of(context)
          .inputDecorationTheme
          .hintStyle : Theme
          .of(context)
          .textTheme
          .bodyText2,
      softWrap: false,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
  }

  /// 下方弹出框
  void _onShowModalBottomSheet() {
    BaseComponentUtils.buildBottomSheet(
      context,
      child: BaseBottomSheetMultipleTemplate(
        controller: widget.controller,
        onComplete: (v) {
          setState(() {});
          if (widget.onComplete != null) {
            widget.onComplete(widget.controller.selected);
          }
        },
      ),
    );
  }
}
